//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BMASwap.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/BMAIndex.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Swap.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CBMASwap::CBMASwap (QL::Instruments::BMASwap::TypeEnum type, Double nominal, Cephei::QL::Times::ISchedule^ liborSchedule, Double liborFraction, Double liborSpread, Cephei::QL::Indexes::IIborIndex^ liborIndex, Cephei::QL::Times::IDayCounter^ liborDayCount, Cephei::QL::Times::ISchedule^ bmaSchedule, Cephei::QL::Indexes::IBMAIndex^ bmaIndex, Cephei::QL::Times::IDayCounter^ bmaDayCount, Cephei::QL::IPricingEngine^ QL_Pricer) : CSwap(CBMASwap::typeid)
{
    CSchedule^ _CliborSchedule;
    CIborIndex^ _CliborIndex;
    CDayCounter^ _CliborDayCount;
    CSchedule^ _CbmaSchedule;
    CBMAIndex^ _CbmaIndex;
    CDayCounter^ _CbmaDayCount;
    try
    {
#ifdef HANDLE
        _phBMASwap = NULL;
#endif
        QuantLib::BMASwap::Type _type = (QuantLib::BMASwap::Type)type ;
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        _CliborSchedule = safe_cast<CSchedule^> (liborSchedule);
        _CliborSchedule->Lock();
        QuantLib::Schedule& _liborSchedule = static_cast<QuantLib::Schedule&> (_CliborSchedule->GetReference ()); 
        QuantLib::Rate _liborFraction = (QuantLib::Rate)ValueHelper::Convert (liborFraction);
        QuantLib::Rate _liborSpread = (QuantLib::Rate)ValueHelper::Convert (liborSpread);
        _CliborIndex = safe_cast<CIborIndex^> (liborIndex);
        _CliborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _liborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CliborIndex->GetShared ()); 
        _CliborDayCount = safe_cast<CDayCounter^> (liborDayCount);
        _CliborDayCount->Lock();
        QuantLib::DayCounter& _liborDayCount = static_cast<QuantLib::DayCounter&> (_CliborDayCount->GetReference ()); 
        _CbmaSchedule = safe_cast<CSchedule^> (bmaSchedule);
        _CbmaSchedule->Lock();
        QuantLib::Schedule& _bmaSchedule = static_cast<QuantLib::Schedule&> (_CbmaSchedule->GetReference ()); 
        _CbmaIndex = safe_cast<CBMAIndex^> (bmaIndex);
        _CbmaIndex->Lock();
        boost::shared_ptr<QuantLib::BMAIndex>& _bmaIndex = static_cast<boost::shared_ptr<QuantLib::BMAIndex>&> (_CbmaIndex->GetShared ()); 
        _CbmaDayCount = safe_cast<CDayCounter^> (bmaDayCount);
        _CbmaDayCount->Lock();
        QuantLib::DayCounter& _bmaDayCount = static_cast<QuantLib::DayCounter&> (_CbmaDayCount->GetReference ()); 
        _ppBMASwap = new boost::shared_ptr<QuantLib::BMASwap> (new QuantLib::BMASwap ( _type,  _nominal,  _liborSchedule,  _liborFraction,  _liborSpread,  _liborIndex,  _liborDayCount,  _bmaSchedule,  _bmaIndex,  _bmaDayCount ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppBMASwap)->setPricingEngine (_QL_Pricer);
        SetSwap (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CliborSchedule != nullptr) _CliborSchedule->Unlock();
        if (_CliborIndex != nullptr) _CliborIndex->Unlock();
        if (_CliborDayCount != nullptr) _CliborDayCount->Unlock();
        if (_CbmaSchedule != nullptr) _CbmaSchedule->Unlock();
        if (_CbmaIndex != nullptr) _CbmaIndex->Unlock();
        if (_CbmaDayCount != nullptr) _CbmaDayCount->Unlock();
    }
}
Cephei::QL::Instruments::CBMASwap::CBMASwap (boost::shared_ptr<QuantLib::BMASwap>& childNative, Object^ owner) : CSwap(CBMASwap::typeid)
{
#ifdef HANDLE
	_phBMASwap = NULL;
#endif
	_ppBMASwap = &childNative;
    _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
}
Cephei::QL::Instruments::CBMASwap::CBMASwap (QuantLib::BMASwap& childNative, Object^ owner) : CSwap(CBMASwap::typeid)
{
#ifdef HANDLE
	_phBMASwap = NULL;
#endif
	_ppBMASwap = new boost::shared_ptr<QuantLib::BMASwap> (&childNative);
    _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
    _BMASwapOwner = owner;
    _SwapOwner = owner;
}

Cephei::QL::Instruments::CBMASwap::CBMASwap (CBMASwap^ copy) : CSwap(CBMASwap::typeid)
{
#ifdef HANDLE
	_phBMASwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBMASwap = new boost::shared_ptr<QuantLib::BMASwap> (copy->GetShared());
        _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
    }
}
Cephei::QL::Instruments::CBMASwap::CBMASwap (System::Type^ t) : CSwap(CBMASwap::typeid)
{
#ifdef HANDLE
	_phBMASwap = NULL;
#endif
	if (!t->IsSubclassOf(CBMASwap::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CBMASwap::CBMASwap (QuantLib::Handle<QuantLib::BMASwap>& childNative, Object^ owner)  : CSwap(CBMASwap::typeid)
{
	_phBMASwap = &childNative;
	_ppBMASwap = &static_cast<boost::shared_ptr<QuantLib::BMASwap>>(childNative.currentLink());
    _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
    _BMASwapOwner = owner;
}
Cephei::QL::Instruments::CBMASwap::CBMASwap (QuantLib::Handle<QuantLib::BMASwap> childNative)  : CSwap(CBMASwap::typeid)
{
	_phBMASwap = &childNative;
	_ppBMASwap = &static_cast<boost::shared_ptr<QuantLib::BMASwap>>(childNative.currentLink());
    _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CBMASwap::CBMASwap (QuantLib::BMASwap childNative)  : CSwap(CBMASwap::typeid)
{
#ifdef HANDLE
	_phBMASwap = NULL;
#endif
	_ppBMASwap = new boost::shared_ptr<QuantLib::BMASwap> (new QuantLib::BMASwap (childNative));
    _ppSwap = new boost::shared_ptr<QuantLib::Swap> (boost::dynamic_pointer_cast<QuantLib::Swap> (*_ppBMASwap));
}
#endif

Cephei::QL::Instruments::CBMASwap::~CBMASwap ()
{
    if (_ppBMASwap != NULL)
    {
	    delete _ppBMASwap;
        _ppBMASwap = NULL;
    }
}
Cephei::QL::Instruments::CBMASwap::!CBMASwap ()
{
    if (_ppBMASwap != NULL)
    {
	    delete _ppBMASwap;
    }
}
QuantLib::BMASwap& Cephei::QL::Instruments::CBMASwap::GetReference ()
{
    if (_ppBMASwap == NULL) throw gcnew NativeNullException ();
	return **_ppBMASwap;
}
boost::shared_ptr<QuantLib::BMASwap>& Cephei::QL::Instruments::CBMASwap::GetShared ()
{
    if (_ppBMASwap == NULL) throw gcnew NativeNullException ();
	return *_ppBMASwap;
}
QuantLib::BMASwap* Cephei::QL::Instruments::CBMASwap::GetPointer ()
{
    if (_ppBMASwap == NULL) throw gcnew NativeNullException ();
	return &**_ppBMASwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BMASwap>& Cephei::QL::Instruments::CBMASwap::GetHandle ()
{
	if (_phBMASwap == NULL)
	{
		_phBMASwap = new Handle<QuantLib::BMASwap> (*_ppBMASwap);
	}
	return *_phBMASwap;
}
#endif
bool Cephei::QL::Instruments::CBMASwap::HasNative () 
{
	return (_ppBMASwap != NULL);
}

Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CBMASwap::BmaLeg::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppBMASwap)->bmaLeg ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::BmaLegBPS::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->bmaLegBPS ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::BmaLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->bmaLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::FairLiborFraction::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBMASwap)->fairLiborFraction ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::FairLiborSpread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppBMASwap)->fairLiborSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::LiborFraction::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->liborFraction ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CBMASwap::LiborLeg::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppBMASwap)->liborLeg ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::LiborLegBPS::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->liborLegBPS ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::LiborLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->liborLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::LiborSpread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppBMASwap)->liborSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CBMASwap::Nominal::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwap)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Instruments::BMASwap::TypeEnum Cephei::QL::Instruments::CBMASwap::Type::get ()
{
    try
    {
    	QuantLib::BMASwap::Type _rv = (QuantLib::BMASwap::Type)(*_ppBMASwap)->type ( );   
        QL::Instruments::BMASwap::TypeEnum _nrv = (QL::Instruments::BMASwap::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IBMASwap^ Cephei::QL::Instruments::CBMASwap_Factory::Create (QL::Instruments::BMASwap::TypeEnum type, Double nominal, Cephei::QL::Times::ISchedule^ liborSchedule, Double liborFraction, Double liborSpread, Cephei::QL::Indexes::IIborIndex^ liborIndex, Cephei::QL::Times::IDayCounter^ liborDayCount, Cephei::QL::Times::ISchedule^ bmaSchedule, Cephei::QL::Indexes::IBMAIndex^ bmaIndex, Cephei::QL::Times::IDayCounter^ bmaDayCount, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CBMASwap ( type,  nominal,  liborSchedule,  liborFraction,  liborSpread,  liborIndex,  liborDayCount,  bmaSchedule,  bmaIndex,  bmaDayCount,  QL_Pricer);
}
